<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>基于SpringBoot整合SSM案例</title>
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport"><!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header"><h1>商品管理</h1></div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input v-model="pagination.type" placeholder="商品类别" style="width: 200px;"
                          class="filter-item"></el-input>
                <el-input v-model="pagination.name" placeholder="商品名称" style="width: 200px;"
                          class="filter-item"></el-input>
                <el-input v-model="pagination.desc" placeholder="商品描述" style="width: 200px;"
                          class="filter-item"></el-input>
                <el-button @click="getAll()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
            </div>
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="name" label="商品名称" align="center"></el-table-column>
                <el-table-column prop="type" label="商品类别" align="center"></el-table-column>
                <el-table-column prop="price" label="商品单价" align="center"></el-table-column>
                <el-table-column prop="count" label="商品数量" align="center"></el-table-column>
                <el-table-column prop="desc" label="商品描述" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!--分页组件-->
            <div class="pagination-container">
                <el-pagination class="pagiantion" @current-change="handleCurrentChange"
                               :current-page="pagination.currentPage" :page-size="pagination.pageSize"
                               layout="total, prev, pager, next, jumper" :total="pagination.total">
                </el-pagination>
            </div>
            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增商品" :visible.sync="dialogFormVisible">
                    <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="商品名称" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="商品类别" prop="type">
                                    <el-input v-model="formData.type"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="商品单价" prop="price">
                                    <el-input v-model="formData.price"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="商品数量" prop="count">
                                    <el-input v-model="formData.count"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="商品描述">
                                    <el-input v-model="formData.desc" type="textarea"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="cancel()">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog title="编辑检查项" :visible.sync="dialogFormVisible4Edit">
                    <el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="商品名称" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="商品类别" prop="type">
                                    <el-input v-model="formData.type"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="商品单价" prop="price">
                                    <el-input v-model="formData.price"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="商品数量" prop="count">
                                    <el-input v-model="formData.count"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="商品描述">
                                    <el-input v-model="formData.desc" type="textarea"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="cancel()">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script>
    var vue = new Vue({
        el: '#app',
        data: {
            dataList: [],//当前页要展示的列表数据
            dialogFormVisible: false,//添加表单是否可见
            dialogFormVisible4Edit: false,//编辑表单是否可见
            formData: {},//表单数据
            rules: {//校验规则
                type: [{required: true, message: '商品类别为必填项', trigger: 'blur'}],
                name: [{required: true, message: '商品名称为必填项', trigger: 'blur'}],
                price: [{pattern: '^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$', required: true,  message: '商品单价必须为正数', trigger: 'blur'}],
                count: [{pattern: '^[1-9]\d*|0$', required: true,  message: '商品数量必须为正整数', trigger: 'blur'}]
            },
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize: 8,//每页显示的记录数
                total: 0,//总记录数
                type: "",
                name: "",
                desc: ""
            }
        },
        //钩子函数，VUE对象初始化完成后自动执行
        created() {
            this.getAll();
        },
        methods: {
            //列表
            // getAll() {
            //     // 发送异步请求
            //     axios.get("/books").then(res => {
            //         console.log(res.data);
            //         this.dataList = res.data.data;
            //     });
            // },

            //分页查询
            getAll() {
                // 组织参数，拼接url请求地址
                var paramStr = "?name=" + this.pagination.name
                    + "&type=" + this.pagination.type
                    + "&desc=" + this.pagination.desc;
                console.log(paramStr);
                axios.get("/goods/" + this.pagination.currentPage + "/" + this.pagination.pageSize + paramStr).then(res => {
                    // console.log(res.data);
                    this.pagination.currentPage = res.data.data.current;
                    this.pagination.pageSize = res.data.data.size;
                    this.pagination.total = res.data.data.total;
                    this.dataList = res.data.data.records;
                });
            },
            //切换页码
            handleCurrentChange(currentPage) {
                // 修改页码值为当前选中的页码值
                this.pagination.currentPage = currentPage;
                // 执行查询
                this.getAll();
            },
            //弹出添加窗口
            handleCreate() {
                this.dialogFormVisible = true;
                this.resetForm();
            },
            //重置表单
            resetForm() {
                this.formData = {};
            },
            //添加
            handleAdd() {
                axios.post("/goods", this.formData).then(res => {
                    // 判断当前操作是否成功
                    if (res.data.flag) {
                        // 1.关闭弹层
                        this.dialogFormVisible = false;
                        this.$message.success(res.data.msg);
                    } else {
                        this.$message.error(res.data.msg);
                    }
                }).finally(() => {
                    // 2.重新加载数据
                    this.getAll();
                });
            },
            //取消
            cancel() {
                this.dialogFormVisible = false;
                this.dialogFormVisible4Edit = false;
                this.$message.info("当前操作取消！");
            },
            // 删除
            handleDelete(row) {
                // console.log(row);
                this.$confirm("删除的数据将不可恢复，是否继续？", "提示", {type: "info"}).then(() => {
                    axios.delete("/goods/" + row.id).then(res => {
                        if (res.data.flag) {
                            this.$message.success(res.data.msg);
                        } else {
                            this.$message.error(res.data.msg);
                        }
                    }).finally(() => {
                        this.getAll();
                    });
                }).catch(() => {
                    this.$message.info("取消操作");
                });
            },
            //弹出编辑窗口
            handleUpdate(row) {
                // 发送异步请求
                axios.get("/goods/" + row.id).then(res => {
                    // console.log(res.data);
                    if (res.data.flag && res.data.data != null) {
                        this.dialogFormVisible4Edit = true;
                        this.formData = res.data.data;
                    } else {
                        this.$message.error(res.data.msg);
                    }
                }).finally(() => {
                    this.getAll();
                });
            },
            //修改
            handleEdit() {
                axios.put("/goods", this.formData).then(res => {
                    // 判断当前操作是否成功
                    if (res.data.flag) {
                        // 1.关闭弹层
                        this.dialogFormVisible4Edit = false;
                        this.$message.success(res.data.msg);
                    } else {
                        this.$message.error(res.data.msg);
                    }
                }).finally(() => {
                    // 2.重新加载数据
                    this.getAll();
                });
            },
            //条件查询
        }
    })
</script>
</html>